<?php
namespace app\admin\model;

use think\Model;
use think\Cache;

class Site extends Model
{
    private $id = 1;     // 站点信息id；
    public function initialize(){

    }

    /**
     * 站点基础信息
     * 直接从数据库获取
     * */
    public function siteData($id=1){
        $res = $this->find($id);
        //格式化数据
        $res['site_open_opt'] = langOpt(lang('confirm'),$res['site_open']);
        $res['captcha_arr'] = json_decode($res['captcha'],true);
        return $res;
    }

    /**
     * 站点信息
     * 先从缓存获取，如果缓存不存在，再从数据库获取
     * */
    public function info(){
        $res = Cache::get('site');
        if(!$res){
            $res = $this->siteData();
        }
        return $res;
    }


    /**
     * 更新站点信息
     * @param array $data  更新数据
     * @return array
     * */
    public function siteSave($data){
        $where['id'] = 1;
        $res = $this->save($data,$where);

        // 缓存
        $data = $this->siteData()->toArray();
        Cache::set('site',$data);

        return $res;
    }

    /**
     * 验证码信息
     * */
    public function validator($str=null){
        $res = $this->info();
        if($str){
            $result = $res['captcha_arr'][$str];
        }else{
            $result = $res['captcha_arr'];
        }
        return $result;
    }

    /**
     * 格式化站点信息
     * */
    public function formatData(){

    }

    public function f(){
        //
        return 989;
    }

    public function ss2(){
        // 查询单个数据
        $res  = $this->get(1);
        echo $res;
    }

    public function ss111111(){
        $data['name'] = '34344';
        $this->save($data);
    }

    public function ss1111(){
        //$user           = new Site();
        $user = new Site;
        $data['name'] = '89898';
        $user->save($data);
    }
    public function ss111(){
        $user = Site::get(1);
        echo $user['name'];
    }
    public function ss(){
        //$user = new Site();
        //$user = new Site;
        //$user = Model('site');
        $user = $this;
        $res  = $user->get(1);
        echo $res;
    }



}